package com.dld.coupon.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;
import com.constant.net.Constant;

public class CenterActivity extends MapActivity {

	private String lat;
	private String lon;
	private String address;
	private BMapManager mapManager;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setTitle("店连店地图");
		setContentView(R.layout.maplayout);
		String uristr = getIntent().getDataString();

		// 初始化MapActivity
		mapManager = new BMapManager(getApplication());
		// init方法的第一个参数需填入申请的API Key
		mapManager.init("4EE0F29E7EB1FC29EAC2B755CE57511E9535CF98", null);
		super.initMapActivity(mapManager);

		MapView map = (MapView) findViewById(R.id.map_view);
		// 设置地图模式为交通地图
		map.setTraffic(true);
		// 设置启用内置的缩放控件
		map.setBuiltInZoomControls(true);

		map.setClickable(true);
		map.setTraffic(true);

		if (getIntent() != null && getIntent().getExtras() != null) {
			address = getIntent().getExtras().getString(Constant.ADDRESS);
		}

		try {
			if (isValidate(uristr)) {
			} else {
				return;
			}
			double dlat = Double.parseDouble(lat) * 1e6;
			double dlon = Double.parseDouble(lon) * 1e6;

			Log.v("", "center map point  ****  " + dlat + ", " + dlon);

			GeoPoint point = new GeoPoint((int) dlat, (int) dlon);
			map.getController().setCenter(point);
			map.setBuiltInZoomControls(true);
			map.setTraffic(true);
			map.setSaveEnabled(true);
			map.getController().setZoom(16);
			map.getOverlays().add(new MyOverLay(point));
		} catch (Exception e) {
			e.printStackTrace();
			showMessageException();
		}

	}

	private void showMessageException() {
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		AlertDialog ad = adb.create();
		ad.setTitle("异常");
		ad.setMessage("经纬度信息错误，退出店连店地图");
		ad.setButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}

		});

	}

	public boolean isValidate(String uri) {
		Pattern p = Pattern.compile("geo:([^,]+),([\\d\\.]+)");
		Matcher matcher = p.matcher(uri);
		boolean b = matcher.matches();
		if (b) {
			lat = matcher.group(1);
			lon = matcher.group(2);
		}
		return b;

	}

	@Override
	protected boolean isRouteDisplayed() {
		return true;
	}

	@Override
	protected void onDestroy() {
		if (mapManager != null) {
			mapManager.destroy();
			mapManager = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mapManager != null) {
			mapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mapManager != null) {
			mapManager.start();
		}
		super.onResume();
	}

	class MyOverLay extends Overlay {
		private GeoPoint center;
		private Bitmap bmp;

		public MyOverLay(GeoPoint center) {
			this.center = center;
			bmp = ((BitmapDrawable) getResources().getDrawable(
					R.drawable.dingwei)).getBitmap();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			Projection proj = mapView.getProjection();
			Point out = new Point();
			proj.toPixels(center, out);
			Matrix m = new Matrix();
			m.postTranslate(out.x, out.y);
			canvas.drawBitmap(bmp, m, null);
			Paint paint = new Paint();
			paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setColor(Color.RED);
			canvas.drawText(address, out.x, out.y + bmp.getHeight(), paint);
		}

	}

}
